﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace StockImport.dataimport.common
{
    [Serializable()]
    public class SBRecords : ISerializable
    {
        public List<SBEntry> SBEntries;
        public SBRecords()
        {
        }
        public SBRecords(SerializationInfo info, StreamingContext ctxt)
        {
            this.SBEntries = (List<SBEntry>)info.GetValue("entries", typeof(List<SBEntry>));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("entries", this.SBEntries);
        }
        public static SBRecords LoadFromFile(String fileName)
        {
            SBRecords result;
            Stream stream = File.Open(fileName, FileMode.Open);
            BinaryFormatter bFormatter = new BinaryFormatter();
            result = (SBRecords)bFormatter.Deserialize(stream);
            stream.Close();
            return result;

        }
        public void SaveToFile(String fileName)
        {
            Stream stream = File.Open(fileName, FileMode.Create);
            BinaryFormatter bFormatter = new BinaryFormatter();
            bFormatter.Serialize(stream, this);
            stream.Close();
        }
    }

    [Serializable()]
    public class SBEntry : ISerializable
    {
        public int seq;
        public String instCode;
        public String instName;
        public decimal price;
        public int buy;
        public int sell;
        public Guid instId = Guid.Empty;

        public SBEntry()
        {
        }
        public SBEntry(SerializationInfo info, StreamingContext ctxt)
        {
            this.seq = (int)info.GetValue("seq", typeof(int));
            this.instCode = (String)info.GetValue("instCode", typeof(String));
            this.instName = (String)info.GetValue("instName", typeof(String));
            this.price = (decimal)info.GetValue("price", typeof(decimal));
            this.buy = (int)info.GetValue("buy", typeof(int));
            this.sell = (int)info.GetValue("sell", typeof(int));
            this.instId = (Guid)info.GetValue("instId", typeof(Guid));
        }
        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("seq", this.seq);
            info.AddValue("instCode", this.instCode);
            info.AddValue("instName", this.instName);
            info.AddValue("price", this.price);
            info.AddValue("buy", this.buy);
            info.AddValue("sell", this.sell);
            info.AddValue("instId", this.instId);
        }
    }
}
